Ti.include('util.js');

function createAnnotation(client) {
	client.address = new Address(client.address, client.city, client.postalCode);
	
	var im = Ti.UI.createView({
		top:0,
		width:100,
		height:30,
		backgroundImage:urlImageCustomer(client.raisonSociale)
	});
	
	var annotation = Ti.Map.createAnnotation({
		latitude:client.latitude,
		longitude:client.longitude,
		leftView:im,
		title:client.raisonSociale,
		subtitle:client.city,
		rightButton:Ti.UI.iPhone.SystemButton.DISCLOSURE,
		data:client
	});
	return annotation;
}

function selectCustomer(e) {
	var clicksource = e.clicksource;
	if (clicksource && clicksource != 'pin') {
		SplitView.selectCustomer(e.source.data);
	}
}


/*----------------------------------------------------------*/
MapView = {};

MapView.get = function(clients, callback) {
	var mapview = Ti.Map.createView({
		mapType: Ti.Map.STANDARD_TYPE,
		location:{latitude:45.915, longitude:6.32, latitudeDelta:0.7, longitudeDelta:0.7}
	});
	
	var client;
	for (var i in clients) {
		// pour supprimer le warning
		if (i >= 0) {
			client = clients[i];
			
			var annotation = createAnnotation(client);
			annotation.addEventListener('click', selectCustomer);
			mapview.addAnnotation(annotation);
		}
	}
	
	return mapview;
};